<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "familyinfo".
 *
 * @property integer $id
 * @property string $stu_id
 * @property string $address
 * @property string $recipient
 * @property integer $postal_code
 * @property integer $economic_status
 * @property integer $load
 */
class FamilyinfoModel extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'familyinfo';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['stu_id'], 'required'],
        		[['stu_id'], 'unique'],
            [['id'], 'integer'],
        		[['postal_code'], 'string', 'max' => 11],
            [['stu_id', 'recipient'], 'string', 'max' => 20],
            [['address'], 'string', 'max' => 200],
        		[['stu_name'], 'string', 'max' => 50],
        	[['economic_status'], 'string', 'max' => 20],
        	[['load'], 'string', 'max' => 20],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
           // 'id' => 'ID',
            'stu_id' => '学号',
        		'stu_name'=>'姓名',
            'address' => '家庭详细通讯地址',
            'recipient' => '收件人',
            'postal_code' => '邮政编码',
            'economic_status' => '家庭经济状况',
            'load' => '助学贷款类型',
        ];
    }



    /**
     * 保存导入excel文件内容
     * @param $data  传入文件内容的数组
     * @return bool  保存成功返回true
     *
     */
    public static function SaveImport($data){

        try{

            foreach ($data as $value){

                $person=new FamilyinfoModel();
                $person->stu_id=$value['学号'].'';//学号要转化为字符型才能通过验证
                $person->stu_name=$value['姓名'].'';
                $person->address=$value['家庭详细通讯地址'].'';
                $person-> recipient=$value['收件人'].'';
                $person->postal_code=$value['邮政编码'].'';
                $person->economic_status=$value['家庭经济状况'].'';
                $person->load=$value['助学贷款类型'].'';

                $person->save();
            }
            return true;

        }catch (\Exception $e){
            throw $e;
        }

    }


}
